package com.bigzero.minio.common.controller;

import com.bigzero.minio.common.utils.MinioUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * minio文件控制类
 *
 * @author fanxinxiong
 * @since 2025-02-07
 */
@RestController
@RequestMapping("/common/minioFile")
public class MinioFileController {

    @Value("${minio.bucket-name}")
    private String bucketName;

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 上传文件
     */
    @PostMapping(value = "/upload")
    public String uploadReport(MultipartFile[] files) {
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            String rename = UUID.randomUUID().toString();
            minioUtil.upload(file, rename, bucketName);
        }
        return "上传成功";
    }

    /**
     * 预览文件
     */
    @GetMapping("/preview")
    public String preview(String fileName) {
        return minioUtil.getFileUrl(fileName, bucketName);
    }

    /**
     * 下载文件
     */
    @GetMapping("/download")
    public void download(String fileName, HttpServletResponse response) {
        minioUtil.download(response, fileName, bucketName);
    }

    /**
     * 删除文件
     */
    @GetMapping("/delete")
    public String delete(String fileName){
        minioUtil.delete(fileName, bucketName);
        return "删除成功";
    }

}
